package com.example.lianguisystem.exceptionHandle;


import com.example.lianguisystem.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jay
 * @Description:
 * @date 2021/6/23 14:58
 */
@RestController
@ControllerAdvice
public class ExceptionAdvice {
    public static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);

    @ResponseBody
    @ExceptionHandler(SystemExceptionNew.class)
    public Result handleException(Exception e) {
        Result result = new Result();
        result.setSuccess(false);
        result.setErrorMsg(e.getMessage());
        e.printStackTrace();
        return result;
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result handleMyException(HttpServletRequest request, HttpServletResponse response, RuntimeException e) throws IOException {
        Result result = new Result();
        result.setSuccess(false);
        result.setErrorMsg(e.getMessage() + "");
        e.printStackTrace();
        return result;
    }

//    @ExceptionHandler(RuntimeException.class)
//    @ResponseBody
//    public HttpServletResponse handleException(HttpServletRequest request, HttpServletResponse response, RuntimeException e) throws IOException {
//        PrintWriter writer = response.getWriter();

//        PrintWriter writer = null;
//        try {
//            writer=response.getWriter();
//            writer.write(e.getMessage());
//
//        } catch (IOException ioException) {
//            ioException.printStackTrace();
//        } finally {
//            if (writer != null) {
//                writer.close();
//            }
//        }
//        return response;
//        Result result = new Result();
//        result.setSuccess(false);
//        result.setErrorMsg(e.getMessage());
//        result.setCode(500);
//        return result;
//        return response;
//    }

//    @ExceptionHandler(BusinessExceptionNew.class)
//    @ResponseBody
//    public Result doBusinessException(Exception e) {
//        Result result = new Result();
//        result.setSuccess(false);
//        result.setErrorMsg("业务异常："+e.getMessage());
//        return result;
//    }

}